namespace KinonekoSoftware.Studio.Pages.Galleries
{
    partial class InstanceGalleryViewModel2
    {
        //-------------------------------------------------
        //
        //          Command Handlers
        //
        //-------------------------------------------------
        private async void DoNewInstanceCommand()
        {
            var catalog = _FilterOfCatalog.Catalog ?? _FilterOfCatalog.Catalogs.First();
            var r       = await (catalog.UseCustomType ? Platform.NewInstance(catalog.CustomType) : Platform.NewInstance(catalog.Type));

            if (!r.IsFinished)
            {
                return;
            }

            //
            // 
            var cache = r.Value;

            //
            // 添加 Cache
            _FinalResult.Add(cache);
            EngineOfInstance.AddCache(cache);

            //
            // 添加Cache到Gallery。
            if (Gallery is not null)
            {
                Gallery.Collection.Add(cache);
                Gallery.Selected = cache;
            }

            var pageBy = GetPageBy();
            var mask   = pageBy - 1;

            PageCount = (_FinalResult.Count + mask) / pageBy;

            //
            // 跳转到指定的页面。
            if (PageIndex != PageCount)
            {
                PageIndex = PageCount;
                Paging(PageIndex);
            }
        }

        private void DoOpenNotionCommand(InstanceCache element)
        {
            this.ThisStory(element);
        }

        private void DoOpenStoryCommand(InstanceCache element)
        {
            this.ThisStory(element);
        }

        private void DoEditInstanceCommand(InstanceCache target)
        {
            if (target is null)
            {
                return;
            }

            Platform.OpenInstance(target);
        }

        private void DoQuikEditInstanceCommand(InstanceCache target)
        {
            if (target is null)
            {
                return;
            }

            IsEditPaneOpen = true;
        }

        private void DoLockOrUnlockInstanceCommand(InstanceCache target)
        {
            if (target is null)
            {
                return;
            }


            target.IsLocked = !target.IsLocked;
            EngineOfInstance.UpdateCache(target);
            PushToView();
        }

        private async void DoRemoveInstanceCommand(InstanceCache target)
        {
            if (target is null)
            {
                return;
            }

            if (target.IsLocked)
            {
                await Warning(TitleSR.WarningOperation, TextSR.Remove_Item_Failed_Locked);
                return;
            }

            if (!await RemoveItem())
            {
                return;
            }

            //
            //
            target.IsDeleted = true;

            Gallery.Collection?.Remove(target);
            EngineOfInstance.RemoveCache(target);

            var pageBy = GetPageBy();
            var mask   = pageBy             - 1;
            PageCount = (_FinalResult.Count + mask) / pageBy;
        }


        #region Quik


        private async void DoSaveInstanceCommand(InstanceCache target)
        {
            if (target is null)
            {
                return;
            }

            //
            //
            EngineOfInstance.UpdateCache(target);

            //
            //
            IsEditPaneOpen = false;

            //
            //
            await Success(TitleSR.SuccessOperation, TextSR.Successful_Save);
        }

        #endregion
        
        
        
        public ICommandEX NewInstanceCommand           { get; }
        public ICommandEX EditInstanceCommand          { get; }
        public ICommandEX SaveInstanceCommand          { get; }
        public ICommandEX OpenNotionCommand            { get; }
        public ICommandEX OpenStoryCommand             { get; }
        public ICommandEX QuikEditInstanceCommand      { get; }
        public ICommandEX LockOrUnlockInstanceCommand  { get; }
        public ICommandEX RemoveInstanceCommand        { get; }
    }
}